#!/bin/bash

script_template_file=scripts/run_basic/sm1000/template.pbs
bench_dir=$1
exec_bin=$2
thirdFilter=$3	# filter by prefix of folder path
#thirdFilter=Two-player-Game

# echo $thirdFilter

extract_after_underscore() {
    local string="$1"
    local underscore_index=$(expr index "$string" "_")

    if [ "$underscore_index" -gt 0 ]; then
        local matched="${string:$underscore_index}"
        echo "$matched"
    else
        echo "$string"
    fi
}

extract_first_digit() {
    local string="$1"
    local regex='[0-9]+.*'

    if [[ $string =~ $regex ]]; then
        local matched="${BASH_REMATCH[0]}"
        echo "$matched"
    else
        echo "no_digit_error"
    fi
}


folder_list=()
folder_list+=("correct_bench1")
folder_list+=("bench2")

folder_ids=()
folder_ids+=("bench1")
folder_ids+=("bench2")


filtered_folder_list=()
filtered_folder_ids=()
for folder_index in "${!folder_list[@]}"
do
	folder="${folder_list[$folder_index]}"
	folder_id="${folder_ids[$folder_index]}"
	if [[ $folder == "$thirdFilter"* ]]; then
        # echo "SelectedElement: $folder"
		filtered_folder_list+=($folder)
		filtered_folder_ids+=($folder_id)
	fi
done

thirdFilterId=""
if [ ${#filtered_folder_list[@]} -eq 1 ]; then
    thirdFilterId="${filtered_folder_ids[0]}"
elif [ -z "$thirdFilter" ]; then
    thirdFilterId="all"
else
    thirdFilterId=$thirdFilter
fi

pwd=$(pwd)
cd $bench_dir

file_name_list=()
file_ids=()
number=0
for folder_index in "${!filtered_folder_list[@]}"
do
	folder="${filtered_folder_list[$folder_index]}"
	folder_id="${filtered_folder_ids[$folder_index]}"
    # echo "Index: $folder_index, Element: $folder, $folder_id"
    filenum=$(ls -1 $folder/*.ltlf 2>/dev/null | wc -l)
    number=$((number + filenum))
done


last_idx=$(( (number + 19) / 20 ))

script_content=$(cat "$pwd/$script_template_file")
new_content=$script_content
new_content=$(echo "$new_content" | sed "s@#PBS -t 0-49@#PBS -t 1-$last_idx@")
new_content=$(echo "$new_content" | sed "s@#PBS -N test-queue@#PBS -N $thirdFilterId@")
new_content=$(echo "$new_content" | sed "s@bench_dir=\$1@bench_dir=$bench_dir@")
new_content=$(echo "$new_content" | sed "s@exec_bin=\$2@exec_bin=$exec_bin@")
new_content=$(echo "$new_content" | sed "s@thirdFilter=\$3@thirdFilter=$thirdFilter@")

bin_id=$(basename $exec_bin)
# thirdFilter_="${thirdFilter//\//-}"
# new_content=$(echo "$new_content" | sed "s@#PBS -o log/output_log@#PBS -o log/output_log_$thirdFilter_@")
# new_content=$(echo "$new_content" | sed "s@#PBS -e log/error_log@#PBS -e log/error_log_$thirdFilter_@")

logdir=$bin_id/$thirdFilter
mkdir -p $pwd/log/$logdir
new_content=$(echo "$new_content" | sed "s@#PBS -o log/output_log@#PBS -o log/$logdir/output@")
new_content=$(echo "$new_content" | sed "s@log_output_root_path=@log_output_root_path=log/$logdir/output@")
new_content=$(echo "$new_content" | sed "s@#PBS -e log/error_log@#PBS -e log/$logdir/error@")
new_content=$(echo "$new_content" | sed "s@log_error_root_path=@log_error_root_path=log/$logdir/error@")


echo "$new_content"
